home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / NeXTtext / btree / BTreeCursor.h < prev    next >
Encoding:
Text File  |  1991-10-07  |  12.6 KB  |  265 lines

  1. /*
  2. BTreeCursor.h
  3. Copyright 1990, NeXT, Inc.
  4. Responsibility: Jack Greenfield
  5.  
  6. DEFINED AS: A common class
  7. HEADER FILES: BTree.h
  8. */
  9.  
  10. #import    <objc/Object.h>
  11.  
  12. @interface BTreeCursor : Object
  13. /*
  14. The BTreeCursor class defines objects that perform operations on BTrees.
  15.  
  16. A BTree is logically an ordered set of \fIkey/record\fR pairs, and a position in a BTree is defined by a specific key.  Each BTreeCursor records a position in a BTree; the key defining its position is stored in a per instance key buffer.  This means that two or more BTreeCursors may be opened on a BTree without difficulty.
  17.  
  18. The lengths of the keys stored in a BTree are limited by the page size of the backing store.  The maximum key length is returned by the \fBmaxKeyLength\fR method.  For efficiency, however, keys should be kept as small as possible.  In principle, the lengths of the records stored in a BTree are limited only by the resolution of a long unsigned integer.  In practice, record lengths are limited by the amount of available disk space. Records smaller than the virtual memory page size are copied; records larger than the page size are memory mapped.
  19.  
  20. For efficiency in working with large records, the BTreeCursor exports several methods for reading and writing ranges of bytes within records.  These methods read and write only the pages affected by the byte range operations.
  21.  
  22. The BTreeCursor class reports errors with NX_RAISE().
  23. */
  24. {
  25.     struct _cstatus        {
  26.         unsigned    performSave:1;
  27.         unsigned    traceNeeded:1;
  28.         unsigned    modified:1;
  29.         unsigned    firstPosition:1;
  30.         unsigned    lastPosition:1;
  31.         unsigned    spanning:1;
  32.         unsigned    rightRotation:1;
  33.         unsigned    leftRotation:1;
  34.         unsigned    RESERVED:8;
  35.     }            _cursorStatus;
  36.     void            *keyBuffer;    /* The key buffer */
  37.     unsigned short        bufferLength;    /* The current length 
  38.                         of the key buffer */
  39.     unsigned short        maxKeyLength;    /* The maximum key length 
  40.                         for this BTree */
  41.     unsigned short        keyLength;    /* The length of the key 
  42.                         in the key buffer */
  43.     void            *_tracePrivate;
  44.     id            btree;        /* The BTree object addressed 
  45.                         by this cursor */
  46.     unsigned long        positionHint;    /* A hint for accelerating 
  47.                         key alignment */
  48.     unsigned long        _syncVersion;
  49.     unsigned long        _traceDepth;
  50. }
  51.  
  52. /* 
  53. METHOD TYPES
  54. Creating and freeing instances
  55. Setting the cursor position
  56. Reading and writing keys and records
  57. Reading and writing byte ranges
  58. Using hints
  59. */
  60.  
  61. + new;
  62. /*
  63. TYPE: Creating and freeing instances; Returns a new BTreeCursor object 
  64.  
  65. Returns a BTreeCursor object for a new BTree residing in virtual memory.  To obtain a BTreeCursor for any other type of BTree, use the \fBcursor\fR method of the BTree class.
  66.  
  67. CF: + new (BTree), - cursor (BTree)
  68. */
  69.  
  70. + _newWith: (void *) private;
  71.  
  72. - (void) _setSpanning: (BOOL) spanning;
  73.  
  74. - (void) _setRightRotation: (BOOL) rightRotation;
  75.  
  76. - (void) _setLeftRotation: (BOOL) leftRotation;
  77.  
  78. - free;
  79. /*
  80. TYPE: Creating and freeing instances; Frees the BTreeCursor object 
  81.  
  82. Frees the storage occupied by the BTreeCursor object.
  83. */
  84.  
  85. - (void) setAutoSave: (BOOL) autoSave;
  86. /*
  87. Enables or disables automatic saving after every modification. Automatic saving may be enforced by the backing store.
  88.  
  89. CF: - autoSave
  90. */
  91.  
  92. - (BOOL) autoSave;
  93. /*
  94. Returns boolean true if automatic saving is enabled. Automatic saving may be enforced by the backing store.
  95.  
  96. CF: - setAutoSave:
  97. */
  98.  
  99. - btree;
  100. /*
  101. Returns a pointer to the BTree addressed by this BTreeCursor.
  102. */
  103.  
  104. - (unsigned short) maxKeyLength;
  105. /*
  106. Returns the maximum key length for the BTree addressed by the cursor.  This  is computed as the page size of the backing store less per page overhead.  For maximum efficiency, keys should be kept as small as possible.
  107. */
  108.  
  109. - (void) setKey: (void *) data length: (unsigned short) dataLength;
  110. /*
  111. TYPE: Setting the cursor position; Sets the cursor position
  112.  
  113. Sets the cursor position to the key defined by \fIdata\fR and \fIdataLength\fR.  The key is blind data; key ordering is determined by a user supplied key comparator.  See the BTree class for more information on the key comparator.
  114.  
  115. CF: - setKey:length:hint
  116. */
  117.  
  118. - (BOOL) getKey: (void **) data length: (unsigned short *) dataLength 
  119.     valid: (BOOL *) validFlag;
  120. /*
  121. TYPE: Reading and writing; Reads the cursor position
  122.  
  123. Aligns the cursor with the \fIkey/record\fR pair at the current cursor position.  If there is no \fIkey/record\fR pair at the current cursor position, this method aligns the cursor with the \fIkey/record\fR pair immediately following the current cursor position, and repositions the cursor to match the alignment.
  124.  
  125. This method then returns the key defining the cursor position in \fI*data\fR and \fI*dataLength\fR, and returns boolean false in \fI*validFlag\fR when the cursor is beyond the last \fIkey/record\fR pair in the BTree.  The return value is boolean true if the cursor postiion did not change.
  126.  
  127. The pointer returned in \fI*data\fR points to the per instance key buffer; it is not guaranteed to remain valid beyond subsequent messages, since the buffer may be moved by \fBrealloc(3)\fR.
  128. */
  129.  
  130. - (BOOL) setFirst;
  131. /*
  132. TYPE: Setting the cursor position; Sets the first position
  133.  
  134. If there is at least one \fIkey/record\fR pair in the BTree, this method returns true and positions the cursor at the first \fIkey/record\fR pair.
  135. */
  136.  
  137. - (void) setEnd;
  138. /*
  139. TYPE: Setting the cursor position; Sets the last position
  140.  
  141. Positions the cursor at the end of the BTree, \fIbeyond\fR the last \fIkey/record\fR pair.  A subsequent \fBsetPrevious\fR message will position the cursor \fIat\fR the last \fIkey/record\fR pair in the BTree.
  142. */
  143.  
  144. - (BOOL) setPrevious;
  145. /*
  146. TYPE: Setting the cursor position; Sets the previous position
  147.  
  148. Positions the cursor at the \fIkey/record\fR pair immediately preceding the current cursor position, and returns boolean false when the cursor is already at the first \fIkey/record\fR pair in the BTree.
  149. */
  150.  
  151. - (BOOL) setNext;
  152. /*
  153. TYPE: Setting the cursor position; Sets the next position
  154.  
  155. Positions the cursor at the \fIkey/record\fR pair immediately following the current cursor position, and returns boolean false when the cursor moves beyond the last \fIkey/record\fR pair in the BTree.
  156. */
  157.  
  158. - (BOOL) read: (void **) data length: (unsigned long *) dataLength
  159.     valid: (BOOL *) validFlag;
  160. /*
  161. TYPE: Reading and writing; Reads the record at the cursor 
  162.  
  163. Aligns the cursor with the \fIkey/record\fR pair at the current cursor position.  If there is no \fIkey/record\fR pair at the current cursor position, this method aligns the cursor with the \fIkey/record\fR pair immediately following the current cursor position, and repositions the cursor to match the alignment.
  164.  
  165. This method then reads the record at the cursor position into \fI*data\fR and \fI*dataLength\fR, and returns boolean false in \fI*validFlag\fR when the cursor is beyond the last \fIkey/record\fR pair in the BTree.  The return value is boolean true if the cursor postiion did not change.
  166.  
  167. If both \fIdata\fR and \fI*data\fR are non-zero, then \fI**data\fR must be a valid memory location, and \fI*dataLength\fR must describe the number of bytes of storage available at \fI**data\fR.  Up to \fI*dataLength\fR bytes of the record are then read or mapped into \fI**data\fR.
  168.  
  169. If \fIdata\fR is non-zero and \fI*data\fR is zero, then a buffer large enough to hold the record is allocated with \fBmalloc(3)\fR.  The entire record is then read or mapped into the buffer, and the record length is returned in \fI*dataLength\fR.  The address of the buffer is returned in \fI*data\fR.  The sender is responsible for freeing the buffer with \fBfree(3)\fR.
  170.  
  171. If \fIdata\fR is zero, then the record length is returned in \fI*dataLength\fR.
  172. */
  173.  
  174. - (BOOL) insert: (void *) data length: (unsigned long) dataLength;
  175. /*
  176. TYPE: Reading and writing; Inserts a record at the cursor 
  177.  
  178. If there is no \fIkey/record\fR pair at the cursor position, this method inserts a new \fIkey/record\fR pair into the BTree at the cursor position.  The record is defined by \fIdata\fR and \fIdataLength\fR; \fIdataLength\fR may be zero.  This method returns boolean true if the insertion was performed.
  179. */
  180.  
  181. - (void) replace: (void *) data length: (unsigned long) dataLength;
  182. /*
  183. TYPE: Reading and writing; Replaces the record at the cursor 
  184.  
  185. Replaces the record at the cursor position using \fIdata\fR and \fIdataLength\fR.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  186. */
  187.  
  188. - (void) remove;
  189. /*
  190. TYPE: Reading and writing; Removes the record at the cursor 
  191.  
  192. Removes the \fIkey/record\fR pair at the cursor position.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  193. */
  194.  
  195. - (void) append: (void *) data length: (unsigned long) dataLength;
  196. /*
  197. TYPE: Reading and writing byte ranges; Appends a byte range
  198.  
  199. Appends the range of bytes defined by \fIdata\fR and \fIdataLength\fR to the record at the cursor position.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  200.  
  201. CF: - insert:length:at:
  202. */
  203.  
  204. - (void) insert: (void *) data length: (unsigned long) dataLength 
  205.     at: (unsigned long) byteOffset;
  206. /*
  207. TYPE: Reading and writing byte ranges; Inserts a byte range
  208.  
  209. Inserts the range of bytes defined by \fIdata\fR and \fIdataLength\fR at \fIbyteOffset\fR in the record at the cursor position.  The portion of the record above \fIbyteOffset\fR is logically shifted to the right.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  210. */
  211.  
  212. - (void) read: (void **) data length: (unsigned long) dataLength 
  213.     at: (unsigned long) byteOffset;
  214. /*
  215. TYPE: Reading and writing byte ranges; Reads a byte range
  216.  
  217. Reads the range of bytes defined by \fIbyteOffset\fR and \fIdataLength\fR from the record at the cursor position into \fI*data\fR.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  218.  
  219. If both \fIdata\fR and \fI*data\fR are non-zero, then \fI**data\fR must be a the starting address of a valid region of memory at least \fIdataLength\fR bytes long.  \fIdataLength\fR bytes of the record beginning at \fIbyteOffset\fR are then read or mapped into \fI**data\fR.
  220.  
  221. If \fIdata\fR is non-zero and \fI*data\fR is zero, then a buffer at least \fIdataLength\fR bytes long is allocated with \fBmalloc(3)\fR.  \fIdataLength\fR bytes of the record beginning at \fIbyteOffset\fR are then read or mapped into the buffer.  The address of the buffer is returned in \fI*data\fR.  The sender is responsible for freeing the buffer with \fBfree(3)\fR.
  222.  
  223. If \fIdata\fR is zero, this method raises an exception.
  224. */
  225.  
  226. - (void) remove: (unsigned long) dataLength at: (unsigned long) byteOffset;
  227. /*
  228. TYPE: Reading and writing byte ranges; Removes a byte range
  229.  
  230. Removes the range of bytes defined by \fIbyteOffset\fR and \fIdataLength\fR from the record at the cursor position.  The portion of the record above \fIbyteOffset\fR is logically shifted to the left.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  231. */
  232.  
  233. - (void) replace: (void *) data length: (unsigned long) dataLength 
  234.     at: (unsigned long) byteOffset;
  235. /*
  236. TYPE: Reading and writing byte ranges; Replaces a byte range
  237.  
  238. Replaces the range of bytes defined by \fIbyteOffset\fR and \fIdataLength\fR in the record at the cursor position with \fIdata\fR.  If the range overruns the end of the record, the excess is silently appended.  If there is no \fIkey/record\fR pair at the cursor position, this method raises an exception.
  239. */
  240.  
  241. - (unsigned long) hint;
  242. /*
  243. TYPE: Using hints; Returns a position hint 
  244.  
  245. Returns a hint describing the cursor position.  The hint may be used with the \fBsetKey:length:hint:\fR method to accelerate the key alignment operations performed by \fBgetKey:length:valid:\fR and \fBread:length:valid:\fR.
  246.  
  247. CF: - setKey:length:hint
  248. */
  249.  
  250. - (void) setKey: (void *) data length: (unsigned short) dataLength 
  251.     hint: (unsigned long) aHint;
  252. /*
  253. TYPE: Using hints; Sets the cursor position with a position hint 
  254.  
  255. Similar to \fBsetKey:length:\fR, but accepts a hint returned by \fBhint\fR.  For relatively static BTrees, the use of hints will significantly reduce the average running time of the key alignment operation performed by \fBgetKey:length:valid:\fR and \fBread:length:valid:\fR.  Hints become less effective as BTrees are modified.  The use of hints may actually increase the average running time of the key alignment operation for highly dynamic BTrees.
  256.  
  257. Hints are especially valuable for improving the performance of secondary key resolution for relatively static BTrees.  Retrieve the hint for each primary \fIkey/record\fR pair after every \fBgetKey:length:valid:\fR, \fBread:length:valid:\fR or \fBinsert:length:\fR message sent to the primary BTree.  If the hint has changed, store the new hint in the secondary record.  At retrieval time, use this method instead of \fBsetKey:length:\fR.
  258.  
  259. CF: - setKey:length:
  260. */
  261.  
  262.  
  263. @end
  264.  
  265.